unit EasemobApi;

interface

uses
  MVCFramework, MVCFramework.Commons,Easemob.Utils,Command,Ecaan.DB;

type

  [MVCPath('/')]
  TEasemobController = class(TMVCController)
  private
    FEasemobApi : TEasemobApi;
  public
//    [MVCPath('/Easemob/($FirstName)')]
//    [MVCHTTPMethod([httpPOST])]
    procedure GetSpecializedHello(const FirstName: String);

    [MVCPath('/Easemob/createUser')]
    [MVCPath('/Easemob/createUser/username/($username)/password/($password)/nickname/($nickname)')]
    [MVCHTTPMethod([httpPOST])]
    procedure createUser(CTX:TWebContext);

    [MVCPath('/Easemob/getUser')]
    [MVCHTTPMethod([httpPOST])]
    procedure getUser(CTX:TWebContext);

    [MVCPath('/Easemob/editNickname')]
    [MVCHTTPMethod([httpPOST])]
    procedure editNickname(CTX:TWebContext);

    [MVCPath('/Easemob/resetPassword')]
    [MVCHTTPMethod([httpPOST])]
    procedure resetPassword(CTX:TWebContext);

    [MVCPath('/Easemob/getUsers')]
    [MVCHTTPMethod([httpPOST])]
    procedure getUsers(CTX:TWebContext);

    [MVCPath('/Easemob/createGroup')]
    [MVCHTTPMethod([httpPOST])]
    procedure createGroup(CTX:TWebContext);

    [MVCPath('/Easemob/modifyGroupInfo')]
    [MVCHTTPMethod([httpPUT])]
    procedure modifyGroupInfo(CTX:TWebContext);

    [MVCPath('/Easemob/deleteGroup')]
    [MVCHTTPMethod([httpDELETE])]
    procedure deleteGroup(CTX:TWebContext);

    [MVCPath('/Easemob/getGroups')]
    [MVCHTTPMethod([httpGET])]
    procedure getGroups(CTX:TWebContext);


    [MVCPath('/Easemob/addGroupMember')]
    [MVCHTTPMethod([httpPOST])]
    procedure addGroupMember(CTX:TWebContext);

    [MVCPath('/Easemob/deleteUser')]
    [MVCHTTPMethod([httpPOST])]
    procedure deleteUser(CTX:TWebContext);


    [MVCPath('/Easemob/getChatRecordForDate')]
    [MVCHTTPMethod([httpPOST])]
    procedure getChatRecordForDate(CTX:TWebContext);

  protected
    procedure OnBeforeAction(Context: TWebContext; const AActionName: string; var Handled: Boolean); override;
    procedure OnAfterAction(Context: TWebContext; const AActionName: string); override;
  end;
implementation

uses
  MVCFramework.Logger,IdHashMessageDigest,System.JSON,System.SysUtils,PushconApp.Command;

procedure TEasemobController.getUser(CTX: TWebContext);
var
 user_name :string;
  JSONObject:TJSONObject;
begin
  user_name:= CTX.Request.Params['username'];
  Render(FEasemobApi.getUser(user_name));
end;

procedure TEasemobController.getUsers(CTX: TWebContext);
 var
  limit :Integer;
  JSONObject:TJSONObject;
begin
   limit := CTX.Request.ParamsAsInteger['limit'];
if limit.ToString.Length=0 then
   begin
   JSONObject :=TJSONObject.ParseJSONValue(CTX.Request.Body) as TJSONObject;
   limit := StrToInt(JSONObject.GetValue('limit').Value);
   end;
 Render(FEasemobApi.getUsers(limit));
end;

procedure TEasemobController.modifyGroupInfo(CTX: TWebContext);
var
  groupname, description, maxusers: string;
   JSONObject:TJSONObject;
begin

   groupname :=CTX.Request.Params['groupname'];
   description:=CTX.Request.Params['description'];
   maxusers:=CTX.Request.Params['maxusers'];
if groupname.Length=0 then
   begin
    JSONObject :=TJSONObject.ParseJSONValue(CTX.Request.Body) as TJSONObject;
    groupname :=JSONObject.GetValue('groupname').Value;
    description:=JSONObject.GetValue('description').Value;
    maxusers:=JSONObject.GetValue('maxusers').Value;
   end;
  Render(FEasemobApi.modifyGroupInfo(groupname, description, maxusers));
end;

procedure TEasemobController.addGroupMember(CTX: TWebContext);
var
   group_id,user_name:string;
    JSONObject:TJSONObject;
begin
// if CTX.Request.Body='' then
//  begin
   group_id:=CTX.Request.Params['group_id'];
   user_name:=CTX.Request.Params['username'];
//  end else
//  begin
//   JSONObject :=TJSONObject.ParseJSONValue(CTX.Request.Body) as TJSONObject;
//    group_id:=JSONObject.GetValue('group_id').Value;
//    user_name:=JSONObject.GetValue('username').Value;
//  end;
   Render(FEasemobApi.addGroupMember(group_id,user_name));
end;

procedure TEasemobController.createGroup(CTX: TWebContext);
var
  groupname, desc, maxusers, owner,members: string;
   JSONObject:TJSONObject;
begin
   groupname :=CTX.Request.Params['groupname'];
   desc:=CTX.Request.Params['desc'];
   maxusers:=CTX.Request.Params['maxusers'];
   owner:=CTX.Request.Params['owner'];
   members:=CTX.Request.Params['members'];
//if groupname.Length=0 then
//  begin
//   JSONObject :=TJSONObject.ParseJSONValue(CTX.Request.Body) as TJSONObject;
//   groupname :=JSONObject.GetValue('groupname').Value;
//   desc:=JSONObject.GetValue('desc').Value;
//   maxusers:=JSONObject.GetValue('maxusers').Value;
//   owner:=JSONObject.GetValue('owner').Value;
//   members:=JSONObject.GetValue('members').Value;
//  end;
  Render(FEasemobApi.createGroup(groupname, desc, maxusers, owner,members));
end;

procedure TEasemobController.createUser(CTX: TWebContext);
var
  user_name, pass_word,nickname:string;
  md5 :TIdHashMessageDigest5;
  JSONObject:TJSONObject;
begin
   user_name :=CTX.Request.Params['username'];
   pass_word:=CTX.Request.Params['password'];
   nickname:=CTX.Request.Params['nickname'];
  try
   md5 :=TIdHashMessageDigest5.Create;
   pass_word := LowerCase(md5.HashStringAsHex(pass_word));
  finally
   md5.Free;
  end;
  Render(FEasemobApi.createUser(user_name, pass_word,nickname));
end;

procedure TEasemobController.deleteGroup(CTX: TWebContext);
var
  JSONObject:TJSONObject;
  group_id:String;
begin
 if CTX.Request.Body='' then
   group_id := CTX.Request.Params['group_id']
  else
  begin
   JSONObject :=TJSONObject.ParseJSONValue(CTX.Request.Body) as TJSONObject;
   group_id:=JSONObject.GetValue('group_id').Value;
  end;
  Render(FEasemobApi.deleteGroup(group_id));
end;

procedure TEasemobController.deleteUser(CTX: TWebContext);
var
  JSONObject:TJSONObject;
  user_name :string;
begin
   user_name := CTX.Request.Params['username'];
   Render(FEasemobApi.deleteUser(user_name));
end;

procedure TEasemobController.editNickname(CTX: TWebContext);
var
 user_name,nickname:String;
  JSONObject:TJSONObject;
begin
  user_name:=CTX.Request.Params['UserName'];
  nickname:=CTX.Request.Params['nickname'];
  Render(FEasemobApi.editNickname(user_name,nickname));
end;

procedure TEasemobController.getChatRecordForDate(CTX: TWebContext);
begin
  Render(FEasemobApi.getChatRecordForDate);
end;

procedure TEasemobController.getGroups(CTX: TWebContext);
 var
  limit :Integer;
  JSONObject:TJSONObject;
begin
 if CTX.Request.Body='' then
 limit := CTX.Request.ParamsAsInteger['limit']
 else
 begin
  JSONObject :=TJSONObject.ParseJSONValue(CTX.Request.Body) as TJSONObject;
  limit:= StrToInt(JSONObject.GetValue('limit').Value);
 end;
 Render(FEasemobApi.getGroups(limit));
end;

procedure TEasemobController.GetSpecializedHello(const FirstName: String);
begin
//  FEasemob.GetAToken;
  Render(FEasemobApi.getUsers(0));
end;

procedure TEasemobController.OnAfterAction(Context: TWebContext; const AActionName: string);
begin
  { Executed after each action }
  FEasemobApi.Free;
  inherited;
end;

procedure TEasemobController.OnBeforeAction(Context: TWebContext; const AActionName: string; var Handled: Boolean);
begin
  { Executed before each action
    if handled is true (or an exception is raised) the actual
    action will not be called }
  inherited;
//  SetHeaders(Context);
  FEasemobApi := TEasemobApi.Create(client_id,client_secret,org_name,app_name,Url);
end;
procedure TEasemobController.resetPassword(CTX: TWebContext);
var
 user_name,newpassword:String;
  Md5:TIdHashMessageDigest5;
  JSONObject:TJSONObject;
begin
  user_name:=CTX.Request.Params['UserName'];
  newpassword:=CTX.Request.Params['newpassword'];
  try
   md5 :=TIdHashMessageDigest5.Create;
   newpassword :=LowerCase(md5.HashStringAsHex(newpassword));
  finally
     md5.Free;
  end;
  LogI('newpassword='+newpassword);
  Render(FEasemobApi.resetPassword(user_name,newpassword));
end;
end.
